package com.wh.simpleredis.config;

import com.wh.simpleredis.pojo.User;
import com.wh.simpleredis.serializer.CustomSerializer;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Auther: HuiEr
 * @Date: 2020/3/10 18:47
 * @Description
 */
@SpringBootConfiguration
public class RedissonConfig {

    @Bean
    public RedissonClient redissonClient(){
        RedissonClient redissonClient = null;
        try {
            Config config = new Config();
            config.useSingleServer().setAddress("redis://134.175.206.191:6379");
            config.useSingleServer().setPassword("xiaobobo");
            redissonClient = Redisson.create(config);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return redissonClient;
    }

    @Bean
    public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object, Object> objectObjectRedisTemplate = new RedisTemplate<>();
        objectObjectRedisTemplate.setConnectionFactory(redisConnectionFactory);
        objectObjectRedisTemplate.setKeySerializer(new StringRedisSerializer());
/*
        objectObjectRedisTemplate.setValueSerializer(new CustomSerializer(User.class));
*/
        objectObjectRedisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer<Object>(Object.class));
        return objectObjectRedisTemplate;
    }
}
